﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using EIS.DatabaseHelper;
using EIS.Obj;
using EIS.ServerAdapter;

namespace EIS.Data
{
    //Class for getting data from server
    public class FunctionData
    {
        public string mainDataTableName;
        public string schemaDataTableName;
        public string readUnreadDataTableName;
        public string layoutDataTableName;
        public string statusDataTableName;
        public FunctionSchema schema;
        public string FunctionID;

        public FunctionData(string functionID)
        {
            FunctionID = functionID;
            mainDataTableName = EISConstant.GetFunctionDataTableName(functionID);
            schemaDataTableName = EISConstant.GetSchemaTableName(functionID);
            //readUnreadDataTableName = EISConstant.GetReadUnreadTableName(functionID);
            layoutDataTableName = EISConstant.GetLayoutTableName(functionID);
            statusDataTableName = EISConstant.GetStatusTableName(functionID);
            ProvideDataTable(functionID);
            schema = new FunctionSchema(functionID);
        }

        public int LoadFromServer()
        {
           
            var dBHandler = new DBHandler();
  
            // Get Stored Time Ticks.

            var ticksLastUpdate = dBHandler.GetMaxTimeStamp(EISConstant.GetFunctionDataTableName(FunctionID));
            
            if ( ticksLastUpdate == 0)
            {
                Error.WriteEventLog("EISTech Quick Actioner: Get Data From Server", "Last Update timestamp of " + FunctionID + " is 0");
            }
            //Get URL.

            //TODO Add Real Last Update Time
            string url = GlobalVariable.Instance.ServerRootURL + "function/" + FunctionID  + @"/" +
                         ticksLastUpdate + @"/" + GlobalVariable.Instance.CurrentToken;

            DataSet ds = RestAdapter.HttpGet(url);
            if (ds.Tables.Count == 0)
            {
                return 0;
            }
            if (ds.Tables[0].TableName.ToUpper() == "ERROR")
            {
                throw new Exception(ds.Tables[0].Rows[0]["errorCode"].ToString() + ds.Tables[0].Rows[0]["errorMessage"].ToString());
            }
            int colNumber = ds.Tables[0].Columns.Count;
            
            var dsToSave = dBHandler.GetEmptyDataSet(mainDataTableName);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                var drToSave = dsToSave.Tables[0].NewRow();
                for (int i = 0; i < colNumber; i++)
                {
                   // ds.Tables[0].Columns[i].DataType.get
                    try
                    {
                        drToSave[ds.Tables[0].Columns[i].ColumnName] = dr[i];
                        
                    }
                    catch (Exception)
                    {
                     // Bypass   
                    }
                    
                }
                drToSave[EISConstant.EISItemUnread] = "Y";
                //drToSave[EISConstant.EISTimeStamp] = fetchTimeTicks;
                dsToSave.Tables[0].Rows.Add(drToSave);

            }
            dBHandler.UpdateRecords(mainDataTableName, dsToSave.Tables[0]);


    /*        //Save Status
            url = GlobalVariable.Instance.ServerRootURL + "functionStatus/" + FunctionID + @"/" +
                         ticksLastUpdate + @"/" + GlobalVariable.Instance.CurrentToken;

            ds = RestAdapter.HttpGet(url);
            if (ds.Tables.Count == 0)
            {
                return 0;
            }
            if (ds.Tables[0].TableName.ToUpper() == "ERROR")
            {
                throw new Exception(ds.Tables[0].Rows[0]["errorCode"].ToString() + ds.Tables[0].Rows[0]["errorMessage"].ToString());
            }
            colNumber = ds.Tables[0].Columns.Count;

            dsToSave = dBHandler.GetEmptyDataSet(statusDataTableName);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                var drToSave = dsToSave.Tables[0].NewRow();
                for (int i = 0; i < colNumber; i++)
                {
                    drToSave[ds.Tables[0].Columns[i].ColumnName] = dr[i];
                }
                
                dsToSave.Tables[0].Rows.Add(drToSave);

            }
            dBHandler.UpdateRecords(statusDataTableName, dsToSave.Tables[0]);
*/
            return ds.Tables[0].Rows.Count;
        }


        private void ProvideDataTable(string functionID)
        {
            var dBHandler = new DBHandler();
            //check if the table exist. 
            dBHandler.CreateTable(functionID);
            //new DBHandler().CreateFunctionReadUnreadIndexTable(functionID);
            dBHandler.CreateFunctionSchemaTable(functionID);
            dBHandler.CreateFunctionStatusTable(functionID);
            dBHandler.CreateFunctionLayoutTable(functionID);
        }
    }

}

